import { CalendarEvent } from './calendar';
import * as moment from 'moment';

/**
 *日程字符串数据资源类
 *
 * @export
 * @class ScheduleStrResource
 */
export class ScheduleStrResource {
    public static readonly StrDateShowFormat = "MM月DD日";
    public static readonly StrDateTimeShowFormat = "YYYY年MM月DD日 HH:mm";

    public static readonly StrSaveSuccessInfo = "保存成功";
    public static readonly StrSaveFailInfo = "保存失败，请稍后再试";
    public static readonly StrContentEmptyInfo = "标题不能为空";
    public static readonly StrNoneBindCustomerInfo = "请选择关联客户";
    public static readonly StrDeleteConfirmInfo = "您确定要删除吗？";
    public static readonly StrDeleteSuccessInfo = "删除成功";
    public static readonly StrDeleteFailInfo = "删除失败，请稍后再试";
    public static readonly StrBackConfirmInfo = "要放弃当前操作吗？";
    public static readonly StrDataNotReadyInfo = "数据尚未准备好，请等一下再试";
    public static readonly StrOk = "确定";
    public static readonly StrCancel = "取消";
    public static readonly StrObjNotExist = "请重新选择";

    public static readonly StrCalendarTimeCrossDayInfo = "一件事两天做，还是拆成两件事吧";
    public static readonly StrCalendarTimePassedInfo = "时间不能倒流，还是看向未来吧";
    public static readonly StrRecordTooShortInfo = "时间太短了，感觉还要还多话要说";
    public static readonly StrRecordTooLongInfo = "都说了60秒了，休息下吧";
}

export class ScheduleStrResourceEn {
    public static readonly StrDateShowFormat = "MM-DD";
    public static readonly StrDateTimeShowFormat = "YYYY-MM-DD HH:mm";

    public static readonly StrSaveSuccessInfo = "Save success";
    public static readonly StrSaveFailInfo = "Save failed";
    public static readonly StrContentEmptyInfo = "Please input the title";
    public static readonly StrNoneBindCustomerInfo = "Please choose a customer";
    public static readonly StrDeleteConfirmInfo = "Confirm to delete?";
    public static readonly StrDeleteSuccessInfo = "Delete success";
    public static readonly StrDeleteFailInfo = "Delete failed";
    public static readonly StrBackConfirmInfo = "Give up saving?";
    public static readonly StrDataNotReadyInfo = "Please wait a moment and try again";
    public static readonly StrOk = "Confirm";
    public static readonly StrCancel = "Cancel";
    public static readonly StrObjNotExist = "Please choose again";

    public static readonly StrCalendarTimeCrossDayInfo = "一件事两天做，还是拆成两件事吧";
    public static readonly StrCalendarTimePassedInfo = "时间不能倒流，还是看向未来吧";
    public static readonly StrRecordTooShortInfo = "时间太短了，感觉还要还多话要说";
    public static readonly StrRecordTooLongInfo = "都说了60秒了，休息下吧";
}

/**
 *日程项
 *
 * @export
 * @class ScheduleItem
 */
export class ScheduleItem {
    /**
     *关联客户
     *
     * @noteType {*}
     * @memberof ScheduleItem
     */
    AssociatedCustomer: any;
    /**
     *标题内容
     *
     * @noteType {string}
     * @memberof ScheduleItem
     */
    Content: string;
    /**
     *绑定类型的值
     *
     * @noteType {number}
     * @memberof ScheduleItem
     */
    BindTypeVal: number = 0;
    /**
     *数据库Id
     *
     * @noteType {number}
     * @memberof ScheduleItem
     */
    Id: number;
    /**
     *是否已完成
     *
     * @noteType {boolean}
     * @memberof ScheduleItem
     */
    IsComplete: boolean = false;
    /**
     *重要级
     *
     * @noteType {number}
     * @memberof ScheduleItem
     */
    Level: number = 0;
    /**
     *唯一值
     *
     * @noteType {string}
     * @memberof ScheduleItem
     */
    UniqueId: string;
    /**
     *备注
     *
     * @noteType {string}
     * @memberof ScheduleItem
     */
    Remark: string;
    /**
     *提前提醒值(分钟)
     *
     * @noteType {number}
     * @memberof ScheduleItem
     */
    ReminderVal: number = 0;
    /**
     *开始时间字符值ISO格式
     *
     * @noteType {string}
     * @memberof ScheduleItem
     */
    StartDatetimeStr: string;
    /**
     *结束时间字符值ISO格式
     *
     * @noteType {string}
     * @memberof ScheduleItem
     */
    EndDatetimeStr: string;

    /**
     * 录音记录
     *
     * @noteType {Array<any>}
     * @memberof ScheduleItem
     */
    VoiceRecords: Array<any> = [];

    /**
     * 原生日历的事项对象
     *
     * @noteType {CalendarEvent}
     * @memberof ScheduleItem
     */
    NativeEvent: CalendarEvent;

    /**
     *用于原生日历的事项标题
     *
     * @readonly
     * @noteType {string}
     * @memberof ScheduleItem
     */
    get Title(): string {
        return `${this.Content}#${this.UniqueId}`;
    }

    /**
     *开始时间Date类型
     *
     * @readonly
     * @noteType {Date}
     * @memberof ScheduleItem
     */
    get StartDatetime(): Date {
        if (this.StartDatetimeStr == null) return new Date();
        return this.isoToDate(this.StartDatetimeStr);
    }

    /**
     *结束时间Date类型
     *
     * @readonly
     * @noteType {Date}
     * @memberof ScheduleItem
     */
    get EndDatetime(): Date {
        if (this.EndDatetimeStr == null) return new Date();
        return this.isoToDate(this.EndDatetimeStr);
    }

    /**
     * Creates an instance of ScheduleItem.
     * @param {Date} [refDate] 起始时间(可空)
     * @param {any} [customer] 关联客户(可空)
     * @memberof ScheduleItem
     */
    public constructor(startTime?: Date, endTime?: Date) {
        //类型转化时无传入参数,直接返回
        if (startTime == null) return;

        if (endTime == null) {
            let currentDate: Date = new Date();
            // if (startTime < currentDate) {
            //     startTime = currentDate;
            // } else {
            //日期为传入,时间为当前
            let newStartTime = moment([startTime.getFullYear(), startTime.getMonth(),
            startTime.getDate(), currentDate.getHours(), currentDate.getMinutes()]).toDate();
            //}
            startTime = moment(this.addTwoHours(newStartTime)).toDate();
            endTime = moment(this.addTwoHours(newStartTime)).toDate();
        } else if (endTime.getDate() != startTime.getDate()) {
            endTime = moment([startTime.getFullYear(), startTime.getMonth(),
            startTime.getDate(), 23, 59]).toDate();
        }
        this.StartDatetimeStr = moment(startTime).format(ScheduleConstData.StrDateTimeFormat);
        this.EndDatetimeStr = moment(endTime).format(ScheduleConstData.StrDateTimeFormat);
    }

    /**
     * 时间增加2小时
     *
     * @private
     * @param {Date} date 需要处理的时间
     * @returns {Date} 处理后的时间
     * @memberof ScheduleItem
     */
    private addTwoHours(date: Date): Date {
        //因默认时间为当前延后2小时,所以大于21点后就会跨天
        if (date.getHours() > 21) {
            date.setHours(23);
        }
        else {
            date.setHours(date.getHours() + 2);
        }

        return date;
    }

    /**
     *iso格式字符时间转Date类型
     *
     * @private
     * @param {any} dateStr iso格式字符时间
     * @returns Date类型数据
     * @memberof ScheduleItem
     */
    private isoToDate(dateStr) {
        var arr = dateStr.split(/[\-\+ :T]/);
        var date = new Date(arr[0], arr[1] - 1, arr[2], arr[3], arr[4]);

        return date;
    }
}

/**
 *日程列表数据
 *
 * @export
 * @class ScheduleListData
 */
export class ScheduleListData {
    /**
     *已完成事项集合
     *
     * @noteType {Array<ScheduleItem>}
     * @memberof ScheduleListData
     */
    CompleteItems: Array<ScheduleItem>;
    /**
     *未完成列表项集合
     *
     * @noteType {Array<ScheduleItem>}
     * @memberof ScheduleListData
     */
    UnCompleteItems: Array<ScheduleItem>;

}

/**
 *日程页类型枚举
 *
 * @export
 * @enum {number}
 */
export enum EnumSchedulePageType {
    /**
     *添加页
     */
    Add = 0,
    /**
     *修改页
     */
    Modify = 1,
    /**
     *详情页
     */
    Detail = 2
}

export class TailProgressUpdateByAgendaDto{
    TailProgress: number;
    AgendaId: number;
}

export class TailProgress{
    /// <summary>
    /// 待跟进
    /// </summary>
    StayBy = 0;
    /// <summary>
    /// 电话约访
    /// </summary>
    Phone = 1;
    /// <summary>
    /// 需求面谈
    /// </summary>
    Interview = 2;
    /// <summary>
    /// 保单面谈
    /// </summary>
    PolicyInterview = 3;
    /// <summary>
    /// 保单提交
    /// </summary>
    Submit = 4;
    /// <summary>
    /// 完成
    /// </summary>
    Complete = 9;
    /// <summary>
    /// 放弃
    /// </summary>
    GiveUp = 10
}

/**
 *日程常量数据
 *
 * @export
 * @class ScheduleConstData
 */
export class ScheduleConstData {
    public static readonly StrTimeFormat = "HH:mm";
    public static readonly StrDateTimeFormat = "YYYY-MM-DDTHH:mm";

    /**
     *日程绑定类型树
     *
     * @static
     * @memberof ScheduleConstData
     */
    public static readonly ScheduleBindTypeTree = [
        {
            Group: "schedule.ScheduleBindTypeTree.CustomerBusiness",
            GroupValue: 0,
            Items: [{
                Text: "schedule.ScheduleBindTypeTree.PhoneCall",
                Value: 0,
                IconSrc: "./assets/images/customers/ico_db_dh.png"
            },
            {
                Text: "schedule.ScheduleBindTypeTree.Visit",
                Value: 1,
                IconSrc: "./assets/images/customers/ico_db_bf.png"
            },
            {
                Text: "schedule.ScheduleBindTypeTree.Notification",
                Value: 2,
                IconSrc: ""
            },
            {
                Text: "schedule.ScheduleBindTypeTree.PhysicalExamination",
                Value: 3,
                IconSrc: ""
            }, {
                Text: "schedule.ScheduleBindTypeTree.Claim",
                Value: 4,
                IconSrc: ""
            }, {
                Text: "schedule.ScheduleBindTypeTree.BirthdayReminder",
                Value: 5,
                IconSrc: "./assets/images/customers/ico_db_bir.png"
            }]
        },
        {
            Group: "schedule.ScheduleBindTypeTree.CompanyBusiness",
            GroupValue: 1,
            Items: [{
                Text: "schedule.ScheduleBindTypeTree.Conference",
                Value: 100,
                IconSrc: ""
            }, {
                Text: "schedule.ScheduleBindTypeTree.Training",
                Value: 101,
                IconSrc: ""
            }]
        },
        {
            Group: "schedule.ScheduleBindTypeTree.CustomerClue",
            GroupValue: 2,
            Items: [{
                Text: "schedule.ScheduleBindTypeTree.CallPhone",
                Value: 1000,
                IconSrc: "./assets/images/customers/ico_db_bir.png"
            }, {
                Text: "schedule.ScheduleBindTypeTree.Interview",
                Value: 1001,
                IconSrc: "./assets/images/customers/ico_db_bir.png"
            }, {
                Text: "schedule.ScheduleBindTypeTree.PolicyInterview",
                Value: 1002,
                IconSrc: "./assets/images/customers/ico_db_bir.png"
            }, {
                Text: "schedule.ScheduleBindTypeTree.Submit",
                Value: 1003,
                IconSrc: "./assets/images/customers/ico_db_bir.png"
            }]
        }

    ];
    /**
     *重要级列表
     *
     * @static
     * @memberof ScheduleConstData
     */
    public static readonly ImportantList = [
        {
            Text: "重要且紧急",
            Color: "red",
            Value: 0,
        }, {
            Text: "重要但不紧急",
            Color: "yellow",
            Value: 1
        }, {
            Text: "非重要但紧急",
            Color: "red",
            Value: 2
        }, {
            Text: "非重要且不紧急",
            Color: "green",
            Value: 3
        }
    ];
    /**
     *提醒时间列表
     *
     * @static
     * @memberof ScheduleConstData
     */
    public static readonly AlertTimeList = [
        {
            Text: "None",
            Value: -1
        },
        {
            Text: "schedule.AlertTimeList.StartTime",
            Value: 0
        },
        {
            Text: "schedule.AlertTimeList.before1",
            Value: 5
        },
        {
            Text: "schedule.AlertTimeList.before2",
            Value: 10
        },
        {
            Text: "schedule.AlertTimeList.before3",
            Value: 15
        },
        {
            Text: "schedule.AlertTimeList.before4",
            Value: 30
        },
        {
            Text: "schedule.AlertTimeList.before5",
            Value: 60
        },
        {
            Text: "schedule.AlertTimeList.before6",
            Value: 120
        },
        {
            Text: "schedule.AlertTimeList.before7",
            Value: 1440
        },
    ];

    /**
     *日程绑定类型列表,树形的扁平化数据集合
     *
     * @private
     * @static
     * @noteType {Array<any>}
     * @memberof ScheduleConstData
     */
    private static ScheduleBindTypeList: Array<any> = null;

    /**
     *根据绑定类型的值获取绑定类型对象
     *
     * @static
     * @param {any} value 绑定类型的值
     * @returns 绑定类型对象
     * @memberof ScheduleConstData
     */
    public static getBindTypeByVal(value) {
        //将树形数据扁平化用于查询
        if (ScheduleConstData.ScheduleBindTypeList == null) {
            ScheduleConstData.ScheduleBindTypeList = [];
            ScheduleConstData.ScheduleBindTypeTree.forEach(v => {
                v.Items.forEach((vv: any) => {
                    vv.GroupValue = v.GroupValue;
                    ScheduleConstData.ScheduleBindTypeList.push(vv);
                });
            })
        }

        let index = ScheduleConstData.ScheduleBindTypeList.findIndex((v) => v.Value == value);
        if (index < 0) {
            return { Text: (localStorage.getItem('lang') === 'zh' ? ScheduleStrResource : ScheduleStrResourceEn).StrObjNotExist, Value: value, IconSrc: "" }
        }

        return ScheduleConstData.ScheduleBindTypeList[index];
    }

    /**
     *通过提醒时间获取提醒时间对象
     *
     * @static
     * @param {any} value 提醒时间
     * @returns 提醒时间对象
     * @memberof ScheduleConstData
     */
    public static getAlertTimeByVal(value) {
        let index = ScheduleConstData.AlertTimeList.findIndex(v => v.Value == value);
        if (index < 0) {
            return { Text: (localStorage.getItem('lang') === 'zh' ? ScheduleStrResource : ScheduleStrResourceEn).StrObjNotExist, Value: value };
        }

        return ScheduleConstData.AlertTimeList[index];
    }

    /**
     *通过重要级的值获取重要级对象
     *
     * @static
     * @param {any} value 重要级的值
     * @returns 提醒时间对象
     * @memberof ScheduleConstData
     */
    public static getLevelByVal(value) {
        let index = ScheduleConstData.ImportantList.findIndex(v => v.Value == value);
        if (index < 0) {
            return { Text: (localStorage.getItem('lang') === 'zh' ? ScheduleStrResource : ScheduleStrResourceEn).StrObjNotExist, Value: value, Color: 'white' };
        }

        return ScheduleConstData.ImportantList[index];
    }
}
